<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form-model :model="queryParam" layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="合同编号">
              <a-input v-model="queryParam['filter[contract_no]']" placeholder="输入合同编号" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="收款账户">
              <a-select
                show-search
                v-model="queryParam['filter[receive_account_type]']"
                placeholder="请选择收款账户"
              >
                <a-select-option v-for="(item,index) in accountOpts" :key="index" :value="index">{{item}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核时间">
                <a-range-picker v-model="queryParam['check_time']" @change="changeCheckTime" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="顾问姓名">
                <a-input v-model="queryParam['filter[adviser_name]']" placeholder="输入顾问姓名" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="审核财务">
                <a-select
                  v-model="queryParam['filter[checker_uid]']"
                  placeholder="请选择审核财务"
                >
                  <a-select-option v-for="(item,index) in financeOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="打款类型">
                <a-select v-model="queryParam['filter[fee_type]']" placeholder="请选择本次打款类型">
                  <a-select-option v-for="(item,index) in typeOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 5px" @click="reset">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operator">
      <s-table
        bordered
        ref="table"
        size="middle"
        :row-key="(record) => record.payment_id"
        :columns="statsColumns"
        :data="statsData"
      >
        <template slot="title">
          <a-row>
            <a-col :span="12">
              打款金额：<a style="color:#f00">{{counts.amount}}</a>
              <a-divider type="vertical" />
              <!--                发票税费：<a style="color:#f00">3160.00</a>-->
              <!--                <a-divider type="vertical" />-->
              代理费：<a style="color:#f00">{{counts.agency}}</a>
            </a-col>
            <a-col :span="12" style="text-align: right">
              <a-button size="small" icon="export" type="primary" @click="onExport('1')">导出业绩</a-button>
              <!--                <a-button size="small" icon="export" type="primary" @click="onExport('2')">导出已付成本业务</a-button>-->
              <!--            <a-button icon="export" type="danger" @click="onExport('2')">三天未开单</a-button>-->
            </a-col>
          </a-row>
        </template>
        <template slot="ix" slot-scope="text,record,index">
          {{index + 1}}
        </template>
        <template slot="contract_no" slot-scope="text,record">
          <a-button type="link" @click="$router.push({ name: 'ContractDetail', params: { id: record.contract_id } })">{{text}}</a-button>
        </template>
        <template slot="amount" slot-scope="text">
          <a-tag color="red">{{text}}</a-tag>
        </template>
        <template slot="agency_fee" slot-scope="text">
          <a-tag color="red">{{text}}</a-tag>
          <!--            <a-divider type="vertical" />-->
          <!--            <a-tag color="red">{{text}}</a-tag>-->
        </template>
        <template slot="refund_fee" slot-scope="text">
          <a-tag color="red">{{text}}</a-tag>
        </template>
        <template slot="type" slot-scope="text">
          <a-tag v-if="text != ''" color="red">{{typeOpts[text]}}</a-tag>
          <a-tag v-else color="red">未标注打款类型</a-tag>
        </template>
        <template slot="adviser_name" slot-scope="text, record">
          <a-tag>{{text}}</a-tag><br>
          <a-tag>{{record.group_type}}组</a-tag><br>
          <a-tag>{{record.leader_name}}</a-tag>
        </template>
      </s-table>
    </div>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import statsColumns from '@/columns/stats'
  import { getStats } from '@/api/stats'
  import apis from '@/api'
  import storage from 'store'

  export default {
    components: {
      STable
    },
    name: 'Pay',
    data () {
      return {
        statsColumns: statsColumns.list,
        statsData: parameters => {
          return getStats(Object.assign(parameters, this.queryParam)).then(res => {
            this.financeOpts = res.result.all_finances
            this.accountOpts = res.result.receive_account_type_opts
            this.counts.agency = res.result.agencyFeeCount
            this.counts.amount = res.result.amountCount
            this.typeOpts = res.result.typeOpts
            return res.result
          })
        },
        queryParam: {},
        adviserOpts: {},
        financeOpts: {},
        accountOpts: {},
        typeOpts: {},
        advanced: true,
        selectedRowKeys: [],
        exportUrl: apis.ExportData,
        accessToken: storage.get('Access-Token'),
        company: storage.get('company'),
        counts: {}
      }
    },
    methods: {
      changeCheckTime (date, dateString) {
        this.queryParam['filter[check_time_start]'] = dateString[0]
        this.queryParam['filter[check_time_end]'] = dateString[1]
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      reset () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      onExport (type) {
        delete this.queryParam['check_time']
        let params = this.exportUrl + '?access_token=' + this.accessToken + '&type=' + type + '&company=' + this.company
        for (const k in this.queryParam) {
          params = params + '&' + k + '=' + this.queryParam[k]
        }
        window.open(process.env.VUE_APP_API_BASE_URL + params)
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
    }
  }
</script>

<style scoped>

</style>
